package com.heima.wemedia.config;
import com.heima.common.constants.message.PublishArticleConstants;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.HashMap;
import java.util.Map;
/**
 * 声明定时发布文章
 * 所需的 所有交换机  队列 及 绑定关系
 **/
@Configuration
public class DeclarePubArticleRabbitConfig {
    /**
     * 发布文章交换机
     */
    @Bean
    public TopicExchange articleTopicExchange(){
        return new TopicExchange(PublishArticleConstants.PUBLISH_ARTICLE_EXCHANGE, true, false);
    }
    /**
     * 死信交换机
     */
    @Bean
    public TopicExchange deadTopicExchange(){
        return new TopicExchange(PublishArticleConstants.DEAD_PUBLISH_ARTICLE_EXCHANGE, true, false);
    }
    /**
     * 死信队列
     */
    @Bean
    public Queue deadPublishArticleQueue(){
        Map<String, Object> args = new HashMap<>(2);
        // x-dead-letter-exchange 声明当前队列绑定的死信交换机
        args.put("x-dead-letter-exchange", PublishArticleConstants.DEAD_PUBLISH_ARTICLE_EXCHANGE);
        args.put("x-dead-letter-routing-key", PublishArticleConstants.PUBLISH_ARTICLE_ROUTE_KEY);
        return QueueBuilder.durable(PublishArticleConstants.DEAD_PUBLISH_ARTICLE_QUEUE).withArguments(args).build();
    }

    /**
     * 声明发布文章队列
     * @return
     */
    @Bean
    public Queue publishArticleQueue(){
        return new Queue(PublishArticleConstants.PUBLISH_ARTICLE_QUEUE, true);
    }
    /**
     * 绑定 发布文章交换机 + 死信队列
     * @return
     */
    @Bean
    public Binding bindingDeadQueue(){
        return BindingBuilder.bind(deadPublishArticleQueue()).to(articleTopicExchange()).with(PublishArticleConstants.PUBLISH_ARTICLE_DEAD_ROUTE_KEY);
    }
    /**
     * 绑定 发布文章交换机 + 发布文章队列
     * @return
     */
    @Bean
    public Binding bindingPublishArticleQueue(){
        return BindingBuilder.bind(publishArticleQueue()).to(articleTopicExchange()).with(PublishArticleConstants.PUBLISH_ARTICLE_ROUTE_KEY);
    }
    /**
     * 绑定 死信交换机 + 发布文章队列
     * @return
     */
    @Bean
    public Binding bindingPublishArticleQueue2(){
        return BindingBuilder.bind(publishArticleQueue()).to(deadTopicExchange()).with(PublishArticleConstants.PUBLISH_ARTICLE_ROUTE_KEY);
    }
}
